﻿using System.Collections;
using static System.Console;

/*
 * 多线程
 */

// 创建Thread对象
Thread download = new Thread(Download);
// 开启线程
download.Start();
WriteLine("Downloading");

// 带参数的
ArrayList list = new ArrayList();
list.Add(1);
list.Add("hello");
list.Add("Thread");
Thread downloadWithPara = new Thread(DownloadWithPara);
downloadWithPara.Start(list);

// 线程池
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadMethod), "参数数据");
WriteLine("主线程继续执行");

void Download()
{
    WriteLine("Download Begin:" + Thread.CurrentThread.ManagedThreadId);
    Thread.Sleep(1000);
    WriteLine("Download End");
}

// 带参数
void DownloadWithPara(object obj)
{
    ArrayList cs = (ArrayList)obj;
    WriteLine("Download Begin:");
    foreach (object o in cs)
    {
        WriteLine(o);
    }

    Thread.Sleep(1000);
    WriteLine("Download End");
}

void ThreadMethod(object stateInfo)
{
    WriteLine("线程开始执行");
    Thread.Sleep(2000);
    WriteLine("线程结束执行");
}

ReadLine();